<?php
/**
 * User: b179a9
 * Date: 10/10/11
 * Time: 5:54 PM
 */

class UsersController extends Zend_Controller_Action
{

    /**
     * PayPal's api url.
     * @var string
     */
    private $_uri = 'https://api-3t.sandbox.paypal.com/nvp';

    /**
     * FlashMessenger
     *
     * @var Lib_Flash
     */
    protected $_flashMessenger = null;

    public function init()
    {
        if ($this->_request->isXmlHttpRequest()) {
            $this->_helper->layout()->disableLayout();
        } else {
            $this->_helper->layout()->setLayout('admin-layout');
        }
        $this->_flashMessenger = new Lib_Flash();
    }

    public function __call($name, $args)
    {
        $this->_forward('index');
    }

    public function indexAction()
    {
        $usersManager = new \Managers\Users();
        $users = $usersManager->getList();
        $this->view->users = $users;
    }

    public function viewAction()
    {
        $id = $this->_request->getParam('id');
        $moneyManager = new \Managers\MoneyOrders();
        $filter = array('user' => $id);
        $orders = $moneyManager->getList($filter);
        $this->view->orders = $orders;
    }

    public function transAction()
    {
        $id = $this->_request->getParam('id');
        $adapter = new Payments_PayPal($this->_uri);
        $body = $adapter->ecGetTransactionInfo($id)->getBody();
        $result = $adapter->parse($body);
        if (preg_match('/success/i', $result['ACK'])) {
            $this->view->result = $result;
        } else {
            $error_key = 'L_LONGMESSAGE';
            $i = 0;
            $key = $error_key . $i;
            while (array_key_exists($key, $result)) {
                $this->_flashMessenger->addMessage($result[$key], Lib_Flash::ERROR, Lib_Flash::POST);
                $i++;
                $key = $error_key . $i;
            }
        }
    }

}
